// SIPLocation.h: interface for the CSIPLocation class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SIPLOCATION_H__05DBC553_FDE3_4E5D_9B70_65A7C43EB241__INCLUDED_)
#define AFX_SIPLOCATION_H__05DBC553_FDE3_4E5D_9B70_65A7C43EB241__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "base.h"
#include "mystring.h"
#include <list>
#include <map>
//#define USE_MYSQL 1
#ifdef USE_MYSQL
#include "db_mysql.h"
#endif
#include "Lock.h"
using namespace std;
/*******************************************************
localtion server database table;
create table location(aor varchar(128) not null,callid varchar(64) not null,seq int not null,q float not null,host varchar(128) not null,port int not null);

*******************************************************/
struct ContactInfo{
	mystring			scheme;
	mystring			username;
	mystring			host;	//nat ip
	int					port;	//nat port
	mystring			displayname;
	float				q;
	unsigned long		cseq;
	mystring			callid;
	time_t				starttime;
	int					expires;
	int					type;
	struct ContactInfo *pnext;
	struct ContactInfo *pprev;
	mystring			UACHost; //pc ip
	int					UACPort; //pc port
};
struct LocationInfo{
	LocationInfo(){pitem = NULL;};
	~LocationInfo(){struct ContactInfo *pdel;while(pitem){pdel = pitem;pitem = pitem->pnext;delete pdel;};};
	mystring aor;	
	mystring username;
	mystring password;
	struct ContactInfo *pitem;
};
class BASE_API CSIPLocation  
{
public:
	void Update();
	bool UpdateLocation(const mystring &aor,const ContactInfo &cti);
	const struct LocationInfo *GetContact(const mystring &aor);
	struct LocationInfo *GetLocation(const mystring &aor);
	static void FreeLocation(struct LocationInfo *pli);
	void RemoveContact(const mystring &aor);
	void RemoveLocation(const mystring &aor);
	unsigned long GetUserSize()
	{
		unsigned long l;
		
		l = m_ContactMap.size();
		
		return l;
	};
	bool AddContact(const mystring &aor,const ContactInfo &ci);	
	mystring GetPassword(const char *pusername);
	map<mystring,struct LocationInfo *>	*GetContactMap(){return &m_ContactMap;};
#ifdef USE_MYSQL
	bool mysqlconnect(const char *host, const char *user, const char *passwd, const char *db, unsigned int port);
#endif
	CSIPLocation();
	virtual ~CSIPLocation();
private:
	mystring ToLower(mystring &str);
	bool UpdateContact(const mystring &aor,const ContactInfo &ci);
private:
	CLock		m_Lock;
#ifdef USE_MYSQL
	db_mysql*	m_mysql;
#endif
	map<mystring,struct LocationInfo *>	m_ContactMap;
	map<mystring,struct LocationInfo *>	m_LocationMap;
};

#endif // !defined(AFX_SIPLOCATION_H__05DBC553_FDE3_4E5D_9B70_65A7C43EB241__INCLUDED_)
